<?php
namespace app\teacher\controller;

use cmf\controller\BaseController;
use app\teacher\validate;
use app\common\model\AppUserModel;
use app\common\logic\Msg;

//老师
class PublicController extends BaseController
{
	protected $user_id;//用户ID
	protected $shcool_id;//学校ID
	public $ids;//同学校的老师用户ID集合

	 public function _initializeView()
    {

        $cmfAdminThemePath    = config('cmf_admin_theme_path');
        $cmfAdminDefaultTheme = config('cmf_admin_default_theme');

        $themePath = "{$cmfAdminThemePath}{$cmfAdminDefaultTheme}";

        $root = cmf_get_root();

        //使cdn设置生效
        $cdnSettings = cmf_get_option('cdn_settings');
        if (empty($cdnSettings['cdn_static_root'])) {
            $viewReplaceStr = [
                '__ROOT__'     => $root,
                '__TMPL__'     => "{$root}/{$themePath}",
                '__STATIC__'   => "{$root}/static",
                '__WEB_ROOT__' => $root
            ];
        } else {
            $cdnStaticRoot  = rtrim($cdnSettings['cdn_static_root'], '/');
            $viewReplaceStr = [
                '__ROOT__'     => $root,
                '__TMPL__'     => "{$cdnStaticRoot}/{$themePath}",
                '__STATIC__'   => "{$cdnStaticRoot}/static",
                '__WEB_ROOT__' => $cdnStaticRoot
            ];
        }

        $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
        config('template.view_base', "$themePath/");
        config('view_replace_str', $viewReplaceStr);
    }
    /**
     * 初始化后台菜单
     */
    public function initMenu()
    {
    }

	//初始化数据
	public function install(){
		$data = AppUserModel::get($this->user_id);
		$this->shcool_id = $schoolId = $data['schoolId'];
		$ids = AppUserModel::where('schoolId',$schoolId)->where('role',2)->field('id')->select()->toArray();
		$array = '';
		foreach ($ids as $key => $value) {
			$array[] = $value['id'];
		}
		$string = implode(',',$array);
		return $string;
	}

	//检查登录
	public function check(){
		$this->user_id = session("USER_ID");
		if(empty($this->user_id)){
			$this->redirect('public/login');die;
		}
	}

	public function login(){
		return $this->fetch('teacher@:login');
	}

	
	/**
	 * [老师提交登录]
	 * @param [type] $[phone] [手机号]
	 * @param [type] $[code] [验证码]
	 * @return [type] [description]
	 */
	public function toLogin(){
		$param = $this->request->param();
		//验证
		$result = $this->validate($param, 'Teacher');
        if ($result !== true) {
            $this->error($result);
        }
       	//短信验证
        $errMsg = cmf_check_verification_code($param['phone'],$param['code']);
		if (!empty($errMsg)) {
			$this->error($errMsg);
		}
	
		$data = AppUserModel::get(['phone'=>$param['phone']]);

		if(empty($data)){
			$this->error('没有该用户!');
		}

		if($data['role'] != 2){
			$this->error('身份不正确!');
		}
		session("USER_ID",$data['id']);
		AppUserModel::where('id',$data['id'])->setInc("use_num");
		db("app_user_log")->insert(['userId'=>$data['id'],'createTime'=>date('Y-m-d H:i:s')]);
		//halt($data['id']);
		$this->success('登录成功', url("teacher/index/index"));
        
	}

	//发送验证码
	public function send_mobile_code(){
	
		$phone = $this->request->param("phone");
		$code = cmf_get_verification_code($phone);
        if (empty($code)) {
            $this->error("验证码发送过多,请明天再试!");
        }
		$sendmsg=new Msg();
		$res=$sendmsg->index0($phone,$code);
		cmf_verification_code_log($phone, $code);
		if($res['code']==1){
			$this->success('验证码发送成功');
		}else{
        	$this->error('验证码发送失败!');
        }
	}

	/*//老师端后台首页
	public function index(){
		$this->check();
		return $this->fetch();
	}*/
}
